unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, ComCtrls;

const
  txtsep = #126;
  tab    = #9;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ListView1: TListView;
    Memo1: TMemo;
    PageControl1: TPageControl;
    Panel1: TPanel;
    RadioGroup1: TRadioGroup;
    StaticText1: TStaticText;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    work_nr : integer;
    function GetWorkChar:string;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  f          : textfile;
  buf1,buf2  : string;
  l          : TListItem;
begin
  ListView1.BeginUpdate;
  ListView1.Clear;

  application.ProcessMessages;

  case radiogroup1.ItemIndex of
    0 : AssignFile(f,'.\dane_in\dplu_in_1.csv');
    1 : AssignFile(f,'.\dane_in\dplu_in_2.csv');
    2 : AssignFile(f,'.\dane_in\dplu_in_3.csv');
  end;

  {$I-}
  reset(f);
  if IOResult <> 0 then
  begin
    ShowMessage('Blad otwierania pliku bazy');
    exit;
  end;
  readln(f,buf1); // odczyt naglowka
  while not eof(f) do
  begin
    statictext1.Caption:=GetWorkChar;
    application.ProcessMessages;

    readln(f,buf1);
    l:=ListView1.Items.Add;

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // NRKASY
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.Caption:=buf2;

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // CENA
    begin
      if buf1[1] in ['0'..'9'] then buf2:=buf2+buf1[1];
      if buf1[1] = '.' then buf2:=buf2+',';
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // JM
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // SYMBOLPTU
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // NRPLU
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // BAZA
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // KF_NAZWA
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // EANNAKASIE
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // INDEKS
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // PCB_NAZWA
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    buf2:='';
    while (buf1 <> '') and (buf1[1] <> txtsep) do  // NAZWADL
    begin
      buf2:=buf2+buf1[1];
      delete(buf1,1,1);
    end;
    if (buf1 <> '') and (buf1[1] = txtsep) then delete(buf1,1,1);
    l.SubItems.Add(buf2);

    application.ProcessMessages;
  {$I+}
  end;
  closefile(f);

  ListView1.EndUpdate;
  ShowMessage('Odczyt zakonczony');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  f   : textfile;
  b   : string;
  i   : integer;
  plu : string;
  cnt : integer;
  eans: string;

  function FormCena(txt : string):string;
  begin
    FormCena:=FloatToStr(StrToFloat(txt)*100);
  end;

begin
  case radiogroup1.ItemIndex of
    0 : assignfile(f,'.\dane_out\1\dplu_in.txt');
    1 : assignfile(f,'.\dane_out\2\dplu_in.txt');
    2 : assignfile(f,'.\dane_out\3\dplu_in.txt');
  end;

  {$I-}
  rewrite(f);
  if IOResult <> 0 then
  begin
    ShowMessage('Problem z tworzeniem pliku dla kasy');
    exit;
  end;
  case radiogroup1.ItemIndex of
    0 : writeln(f,'@1');            // kasa 1
    1 : writeln(f,'@2');            // kasa 2
    2 : writeln(f,'@3');            // kasa 3
  end;
  writeln(f,'@0');
  writeln(f,'@0,1');
  writeln(f,'@');

  i:=0;
  while i < ListView1.Items.Count do
  begin
    b:='#';
    b:=b+ListView1.Items[i].SubItems[3]+chr(9);        { numer PLU}
    b:=b+'0'+chr(9);                                   { 0 - ma byc, wg dokumentacji }
    b:=b+ListView1.Items[i].SubItems[5]+chr(9);        { nazwa towaru }
    b:=b+ListView1.Items[i].SubItems[2]+chr(9);        { symbol PTU }
    b:=b+FormCena(ListView1.Items[i].SubItems[0])+chr(9); { cena }
    b:=b+'0'+chr(9);                                   { numer opakowania }
    b:=b+'0'+chr(9);                                   { format ilosci }
    b:=b+'1'+chr(9);                                   { flaga_rabat }
    b:=b+'0'+chr(9);                                   { numer_rabatu }
    b:=b+'1'+chr(9);                                   { flaga_ceny }
    b:=b+'1'+chr(9);                                   { grupa }
    b:=b+'0'+chr(9);                                   { jm }
    b:=b+'0'+chr(9);                                   { stan }
    b:=b+'0'+chr(9);                                   { stan_min }

    plu:=ListView1.Items[i].SubItems[3];
    cnt:=0;
    eans:='';
    while (i < ListView1.Items.Count) and (plu = ListView1.Items[i].SubItems[3]) do
    begin
      inc(cnt);
      eans:=eans+ListView1.Items[i].SubItems[6]+chr(9);
      inc(i);
    end;
    delete(eans,length(eans),1); // usuniecie ostatniego taba
    b:=b+IntToStr(cnt)+chr(9)+eans;
    writeln(f,b);
  end;
  closefile(f);
  ShowMessage('Eksport pliku zakonczony');
  {$I+}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  work_nr:=1;
end;

function TForm1.GetWorkChar: string;
const
  txt : string = '|/-\';
begin
  GetWorkChar:=txt[work_nr];
  inc(work_nr);
  if work_nr >= 5 then work_nr:=1;
end;

end.

